<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增商品分类')" />
    <th:block th:include="include :: jasny-bootstrap-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-category-add">
            <input id="treeId" name="parentId" type="hidden" th:value="${dept.id}"/>
            <div class="form-group">
                <label class="col-sm-3 control-label">上级分类：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input class="form-control" type="text" onclick="selectProductCategoryTree()" id="treeName" readonly="true" th:value="${dept.name}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">名称：</label>
                <div class="col-sm-8">
                    <input name="name" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">是否置顶：</label>
                <div class="col-sm-3">
                    <label class="toggle-switch switch-solid">
                        <input type="checkbox" id="IsTop" checked>
                        <span></span>
                    </label>
                </div>
                <label class="col-sm-3 control-label">是否上架：</label>
                <div class="col-sm-3">
                    <label class="toggle-switch switch-solid">
                        <input type="checkbox" id="IsMarketable" checked>
                        <span></span>
                    </label>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">展示图片：</label>
                <div class="col-sm-8">
                    <input id="fileinput-input" name="image" class="form-control" type="text" style="display: None">
                    <div class="fileinput fileinput-new" data-provides="fileinput">
                        <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px;"></div>
                        <div>
                                                        <span class="btn btn-white btn-file"><span class="fileinput-new">选择图片</span><span class="fileinput-exists">更改</span>
                                                            <input name="fileinput-1" id ="fileinput-1" type="file">
                                                        </span>
                            <a href="#" class="btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">排序：</label>
                <div class="col-sm-8">
                    <input name="order" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: jasny-bootstrap-js" />
    <script type="text/javascript">
        var prefix = ctx + "normal/category"
        $("#form-category-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var isTop = $("input[id='IsTop']").is(':checked') == true ? 1 : 0;
                var isMarketable = $("input[id='IsMarketable']").is(':checked') == true ?1 : 0;
                var data = $.param({'isTop': isTop}) + '&' +
                    $.param({'isMarketable': isMarketable}) + '&' +
                    $('#form-category-add').serialize();
                $.operate.save(prefix + "/add", data);
            }
        }

        $(document).ready(function () {
            $("#fileinput-1").fileinput({
                'name':'text-1',
                'clearName':true
            });
        });

        function selectProductCategoryTree() {
            var treeId = $("#treeId").val();
            var productCategoryId = treeId > 0 ? treeId : 0;
            var url = prefix + "/selectProductCategoryTree/" + productCategoryId;
            var options = {
                title: '分类选择',
                width: "380",
                url: url,
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }
        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
        }

        $("#fileinput-1").on("change.bs.fileinput", function (event, data, previewId, index) {
            var file =  this.files[0];
            if (file == null || file == undefined){
                alert("置空");
                return;
            }
            var formFile = new FormData();
            formFile.append("action", "UploadVMKImagePath");
            formFile.append("file", file); //加入文件对象
            var data = formFile;
            $.ajax({
                url: "/common/upload",
                type: "POST",
                data:data,
                dataType: "json",
                cache:false,         //不设置缓存
                processData: false,  // 不处理数据
                contentType: false,   // 不设置内容类型
                success: function (result) {
                    console.log(result);
                    if(result.code == 0){
                        $('#fileinput-input').val(result.url);
                        alert("上传完成!");
                    }else{
                        alert("上传失败");
                    }
                },
            });
        });
    </script>
</body>
</html>